package com.medicinemanager.sql;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.medicinemanager.bean.Entity;
import com.medicinemanager.bean.Staff;
import com.medicinemanager.bean.Medicineinfo;

public class MedicineManager implements TableManager {

	private SQLiteDatabase mSQLiteDatabase = null;

	public MedicineManager(Context context) {
		mSQLiteDatabase = new SQLManager(context).getWritableDatabase();
	}

	@Override
	public Cursor search(String... strings) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean insert(Entity entity) {
		Medicineinfo medicine = (Medicineinfo) entity;
		mSQLiteDatabase.beginTransaction();
		try {
			mSQLiteDatabase
					.execSQL(
							"insert into medicine_information values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
							new Object[] { medicine.getId(),
									medicine.getName(), medicine.getFactory(),
									null, null,
									medicine.getUseage(), medicine.getPrice(),
									medicine.getNumber(), medicine.getPerson(),
									medicine.getPersonid() });
			mSQLiteDatabase.setTransactionSuccessful();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			mSQLiteDatabase.endTransaction();
		}
		return false;
	}

	@Override
	public boolean delete(Entity entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Entity entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList<Entity> rawQuery(String string) {
		ArrayList<Entity> result = null;
		try{
			result = new ArrayList<Entity>();
			Cursor cursor = mSQLiteDatabase.rawQuery(string, null);
			while(cursor.moveToNext()){
				Medicineinfo medicine = new Medicineinfo();
				medicine.setFactory(cursor.getString(cursor.getColumnIndex("factory")));
				medicine.setId(cursor.getString(cursor.getColumnIndex("id")));
				medicine.setName(cursor.getString(cursor.getColumnIndex("name")));
				medicine.setNumber(cursor.getInt(cursor.getColumnIndex("number")));
				medicine.setPerson(cursor.getString(cursor.getColumnIndex("person")));
				medicine.setPersonid(cursor.getString(cursor.getColumnIndex("personid")));
				medicine.setPrice(cursor.getFloat(cursor.getColumnIndex("price")));
				medicine.setUseage(cursor.getString(cursor.getColumnIndex("useage")));
				result.add(medicine);
			}
		}catch(SQLException e){
			
		}
		return result;
	}

	@Override
	public boolean execSQL(String... strings) {
		// TODO Auto-generated method stub
		return false;
	}

}
